package bigbrother.modules.logging;

import bigbrother.command.AbstractCommand;
import bigbrother.log.Log;
import bigbrother.log.logentries.MessageEntry;
import bigbrother.modules.AbstractModule;
import org.apache.wicket.Component;
import org.joda.time.DateTime;

/**
 * A module that logs the conversation in the IRC channel(s) that the bot is in to files.  Also provides a web interface
 * that allows viewing the IRC logs.
 *
 * @author Hans Haggstrom
 */
public final class LogModule
        extends AbstractModule
{

    //======================================================================
    // Public Methods

    //----------------------------------------------------------------------
    // Constructors

    public LogModule()
    {
        super( "log" );

        registerCommand( new AbstractCommand( "bookmark",
                                              "Adds a bookmark with the specified description at the current location of the log.  \n" +
                                              "Optionally specify how many minutes back the bookmark should be placed, using the -<nn>m option.\n" +
                                              "E.g:  !log.bookmark -15m Discussion about how to implement bookmarks." )
        {

            public String doCommand( final String channel,
                                     final String sender,
                                     final String login,
                                     final String hostname,
                                     final String parameters )
            {
                final String[] strings = parameters.split( " " );

                final int minutesAgo = parseMinutesParameter( strings[ 0 ] );

                final String bookmarkName = determineBookmarkName( parameters, strings, minutesAgo );

                placeBookmark( minutesAgo, bookmarkName );

                final String time = minutesAgo == 0 ? "current time" : "" + minutesAgo + " minutes ago";
                return "Placed bookmark titled '" + bookmarkName + "' at " + time + ".";
            }

        } );
    }

    //======================================================================
    // Protected Methods

    protected void onModuleStarted()
    {
/*
        throw new UnsupportedOperationException( "This method has not yet been implemented." ); // IMPLEMENT
*/
    }


    protected void onModuleStopped()
    {
/*
        throw new UnsupportedOperationException( "This method has not yet been implemented." ); // IMPLEMENT
*/
    }

    public void onMessage( final String channel,
                           final String sender,
                           final String login,
                           final String hostname,
                           final String message )
    {
        System.out.println( "LogModule.onMessage - logging" );

        final DateTime stamp = new DateTime();
        System.out.println( "stamp = " + stamp );

        final MessageEntry messageEntry = new MessageEntry( stamp, sender, channel, message );
        System.out.println( "messageEntry = " + messageEntry );

        final Log log = getContext().getLog();
        System.out.println( "log = " + log );

        log.appendEntry( messageEntry );
    }

    //======================================================================
    // Private Methods

    private void placeBookmark( final int minutesAgo, final String bookmarkName )
    {
        throw new UnsupportedOperationException( "This method has not yet been implemented." ); // IMPLEMENT
    }


    private String determineBookmarkName( final String parameters, final String[] strings, final int minutesAgo )
    {
        String bookmarkName = parameters;
        if ( minutesAgo > 0 )
        {
            bookmarkName = "Bookmark";
            if ( strings.length >= 2 )
            {
                bookmarkName = strings[ 1 ];
            }
        }
        return bookmarkName;
    }


    private int parseMinutesParameter( final String optionMaybe )
    {
        int minutes = 0;
        if ( optionMaybe != null )
        {
            if ( optionMaybe.startsWith( "-" ) && optionMaybe.endsWith( "m" ) )
            {
                String numberMaybe = optionMaybe.substring( 1, optionMaybe.length() - 2 );
                try
                {
                    minutes = Integer.parseInt( numberMaybe );
                }
                catch ( NumberFormatException e )
                {
                    // Wasn't number
                    minutes = 0;
                }
            }
        }
        if ( minutes < 0 )
        {
            minutes = 0;
        }
        return minutes;
    }

    public synchronized Component createWebComponent( final String id )
    {
        return null;
    }
}
